﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PromoCodes_Models.Entities;
using PromoCodes_Models.Abstract;

namespace PromoCodes_Models.Concrete
{
    public class EFProductRepository : IRepository<Product>
    {
        private EFDbContext context = EFDbContext.Context;


        public IQueryable<Product> Items
        {
            get { return context.Products; }
        }

        public int Edit(Product item)
        {
            int idChanged = item.ProductID;
            if (item != null)
            {
                if (item.ProductID == 0)
                {
                    context.Products.Add(item);
                    context.SaveChanges();
                    idChanged = context.Products.First(p => p.ProductName == item.ProductName).ProductID;
                }
                else
                {
                    context.Entry(context.Products.SingleOrDefault(p => p.ProductID == item.ProductID)).CurrentValues.SetValues(item);
                }
            }
            context.SaveChanges();
            return idChanged;
        }

        public void Delete(Product item)
        {
            if (item != null)
                context.Products.Remove(item);
            context.SaveChanges();
        }
    }
}
